<template>
  <div class="about">
    <h1>手写深拷贝</h1>
  </div>
</template>
<script setup lang="ts">
let obj1 = {
  name: 'heyongtao',
  adress: {
    city: '深圳'
  }
}
let obj2 = deepClone(obj1)
obj2.name = '123'
console.log(obj1, 'obj1')

function deepClone(obj) {
  // 判断是否是引用数据类型
  if (typeof obj !== 'object' || obj === null) {
    return obj
  }
  //   判断引用类型中是否是数组、对象
  let result
  if (obj instanceof Array) {
    result = []
  } else {
    result = {}
  }

  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      // 递归
      result[key] = deepClone(obj[key])
    }
  }
  return result
}
</script>
<style></style>
